package cz.muni.fi.pa165.travelagency.entities;

import cz.muni.fi.pa165.travelagency.api.entities.IOrder;
import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.entities.ITour;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * @author Lukas Jungmann
 * @version 1.0
 */
@Entity
@NamedQueries({
    @NamedQuery(name = "PackageTour.findAll", query = "SELECT p FROM PackageTour p"),
    @NamedQuery(name = "PackageTour.findById", query = "SELECT p FROM PackageTour p WHERE p.id = :id"),
    @NamedQuery(name = "PackageTour.findByCapacity", query = "SELECT p FROM PackageTour p WHERE p.capacity = :capacity"),
    @NamedQuery(name = "PackageTour.findByDescription", query = "SELECT p FROM PackageTour p WHERE p.description = :description"),
    @NamedQuery(name = "PackageTour.findByDestination", query = "SELECT p FROM PackageTour p WHERE p.destination = :destination"),
    @NamedQuery(name = "PackageTour.findByEnddate", query = "SELECT p FROM PackageTour p WHERE p.end = :end"),
    @NamedQuery(name = "PackageTour.findByName", query = "SELECT p FROM PackageTour p WHERE p.name = :name"),
    @NamedQuery(name = "PackageTour.findByMaxPrice", query = "SELECT p FROM PackageTour p WHERE p.pricePerPerson <= :maxPrice"),
    @NamedQuery(name = "PackageTour.findByPriceperperson", query = "SELECT p FROM PackageTour p WHERE p.pricePerPerson = :pricePerPerson"),
    @NamedQuery(name = "PackageTour.findByStartdate", query = "SELECT p FROM PackageTour p WHERE p.start = :start"),
    @NamedQuery(name = "PackageTour.findByAttributes", query = "SELECT p FROM PackageTour p WHERE p.destination LIKE :destination and p.start >= :start and p.end <= :end and p.capacity >= :capacity and p.pricePerPerson <= :maxPrice")
})
public class PackageTour implements IPackageTour {
    private static final long serialVersionUID = 3L;
    
    @Id
    @GeneratedValue
    private Long id;
    @OneToMany(targetEntity = Tour.class, mappedBy = "associatedTour", cascade = {CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.DETACH})
    private List<ITour> availableTours = new ArrayList<>();
    @OneToMany(targetEntity = Order.class, mappedBy = "orderedPackageTour", cascade = {CascadeType.REFRESH, CascadeType.REMOVE, CascadeType.MERGE, CascadeType.DETACH})
    private List<IOrder> orders = new ArrayList<>();
    private String name;
    private int capacity;
    private String description;
    private String destination;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "startDate")
    private Date start;
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "endDate")
    private Date end;
    private BigDecimal pricePerPerson;

    public PackageTour() {
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public List<ITour> getAvailableTours() {
        return availableTours;
    }

    @Override
    public void setAvailableTours(List<ITour> availableTours) {
        this.availableTours = availableTours;
    }

    @Override
    public List<IOrder> getOrders() {
        return orders;
    }

    @Override
    public void setOrders(List<IOrder> orders) {
        this.orders = orders;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int getCapacity() {
        return capacity;
    }

    @Override
    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String getDestination() {
        return destination;
    }

    @Override
    public void setDestination(String destination) {
        this.destination = destination;
    }

    @Override
    public Date getStart() {
        return start;
    }

    @Override
    public void setStart(Date start) {
        this.start = start;
    }

    @Override
    public Date getEnd() {
        return end;
    }

    @Override
    public void setEnd(Date end) {
        this.end = end;
    }

    @Override
    public BigDecimal getPricePerPerson() {
        return pricePerPerson;
    }

    @Override
    public void setPricePerPerson(BigDecimal pricePerPerson) {
        this.pricePerPerson = pricePerPerson;
    }

    @Override
    public String toString() {
        return "PackageTour{" + "id=" + id + ", name=" + name + ", capacity=" + capacity + ", description=" + description + ", destination=" + destination + ", start=" + start + ", end=" + end + ", pricePerPerson=" + pricePerPerson + '}';
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PackageTour other = (PackageTour) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
}